//
//  AddYunDanChooseAddressVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/9/18.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "AddYunDanChooseAddressVC.h"
#import "AddYunDanChooseAddressCell.h"

@interface AddYunDanChooseAddressVC ()<UITableViewDelegate,UITableViewDataSource> {
//    int page;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation AddYunDanChooseAddressVC

-(NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - BottomHeight - NaviHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = HexColor(0xffffff);
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//        __weak_self;
//        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//            __strong_self;
//            strongSelf->page = 1;
//            [strongSelf requestForList];
//        }];
//        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//            __strong_self;
//            strongSelf->page++;
//            [strongSelf requestForList];
//        }];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"收货地址";
    self.view.backgroundColor = HexColor(0xffffff);
//    page = 1;
    [self.view addSubview:self.tableView];
    [self requestForList];
}

- (void)requestForList {
    [HttpRequest PostWithApi:ReceiptDeliveryAddYunDanGetAddressList params:@{@"t_id":self.t_id,@"dp_ids":self.dp_ids,@"plw_id":self.plw_id} progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
//            if (self->page == 1) {
                [self.dataArray removeAllObjects];
                self.dataArray = [AddYunDanTiHuoAddressModel mj_objectArrayWithKeyValuesArray:responseObject];
//            }else {
//                NSArray *array = [AddYunDanTiHuoAddressModel mj_objectArrayWithKeyValuesArray:responseObject];
//                [self.dataArray addObjectsFromArray:array];
//            }
//            BOOL has_more = [responseObject[@"has_more"] boolValue];
//            if (has_more) {
//                self.tableView.mj_footer.hidden = NO;
//            }else {
//                self.tableView.mj_footer.hidden = YES;
//            }
            [self.tableView reloadData];
        }
//        [self.tableView.mj_header endRefreshing];
//        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError * _Nonnull error) {
//        [self.tableView.mj_header endRefreshing];
//        [self.tableView.mj_footer endRefreshing];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddYunDanChooseAddressCell *cell = [AddYunDanChooseAddressCell AddYunDanChooseAddressCellWithTableView:tableView];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddYunDanChooseAddressCell *cell = [AddYunDanChooseAddressCell AddYunDanChooseAddressCellWithTableView:tableView];
    cell.model = self.dataArray[indexPath.row];
    return cell.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.finishBlock) {
        self.finishBlock(self.dataArray[indexPath.row]);
        [self.rt_navigationController popViewControllerAnimated:YES];
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = HexColor(0xffffff);
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
